package com.lxy.demo.controller;

import com.github.pagehelper.PageInfo;
import com.lxy.demo.entity.ReplyDetail;
import com.lxy.demo.service.ReplyDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Component
@RequestMapping("replyDetail")
public class ReplyDetailController {


    @Autowired
    private ReplyDetailService replyDetailService;

    @RequestMapping("main/{id}")
    public String main(@PathVariable("id") Integer id,
                       @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                       Model model) {
        PageInfo pageInfo = replyDetailService.selectReply(id, pageNum);
        model.addAttribute("id",id);
        model.addAttribute("pageInfo",pageInfo);
        return "reply";
    }

    @RequestMapping("addUI/{id}")
    public String addUI(@PathVariable("id") Integer id,Model model){
        model.addAttribute("id",id);
        return "add";
    }

    @RequestMapping("/add")
    public String add(ReplyDetail replyDetail,Model model,Integer invid){
        Date createdate = new Date();
        replyDetail.setCreatedate(createdate);
        boolean b = replyDetailService.addReply(replyDetail);
        model.addAttribute("id",invid);
        if (b){
            model.addAttribute("add","ok");
        }else {
            model.addAttribute("add","no");
        }
        return "forward:/replyDetail/addUI/"+invid;
    }


}
